package com.example.red_tea_house.moudle.LoginAndRegister.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.red_tea_house.MyApplication;
import com.example.red_tea_house.R;
import com.example.red_tea_house.annotation.HandlerType;
import com.example.red_tea_house.entity.UserBean;
import com.example.red_tea_house.moudle.LoginAndRegister.controller.GetVertifyController;
import com.example.red_tea_house.moudle.LoginAndRegister.controller.RegisterController;
import com.example.red_tea_house.util.ToastUtil;


public class RegisterActivity extends AppCompatActivity {

    private EditText Register_phone;
    private EditText Input_identifying_code;
    private EditText Setting_password_1;
    private EditText Setting_password_2;
    private Button Get_identifying_code;
    private Button Btn_register_register;
    private Button ReturnButton;
    private Button NoregisterReturnButton;
    private Button ForgetPasswordButton;

    private int register_state;
    private String str_phone;
    private String str_identifyCode;
    private String str_password1;
    private String str_password2;

    private RegisterController registerController=new RegisterController(RegisterActivity.this);

    Context context = MyApplication.getContext();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        bindItems();

        monitorGetIdentifyCodeButton();
        monitorRegisterButton();

        turnintoLoginPage();
        turnintoForgetPasswordPage();

    }
    private void bindItems(){
        Register_phone=findViewById(R.id.register_phone);
        Input_identifying_code=findViewById(R.id.input_identifying_code);
        Setting_password_1=findViewById(R.id.setting_password_1);
        Setting_password_2=findViewById(R.id.setting_password_2);
        Get_identifying_code =findViewById(R.id.gain_identifying_code);
        Btn_register_register=findViewById(R.id.btn_register_register);
        NoregisterReturnButton=findViewById(R.id.return_login_layout__from_register);
        ForgetPasswordButton=findViewById(R.id.find_back_password_from_register);
    }
    private void monitorGetIdentifyCodeButton(){
        Get_identifying_code.setOnClickListener((v)->{
            registerController.getIdentifycode(Register_phone.getText().toString());
        });
    }

    private void monitorRegisterButton(){
        Btn_register_register.setOnClickListener((v)->{
            str_phone=Register_phone.getText().toString();
            str_identifyCode=Input_identifying_code.getText().toString();
            str_password1=Setting_password_1.getText().toString();
            str_password2=Setting_password_2.getText().toString();

            if(registerController.setRegisterInfo(str_phone, str_password1,str_password2)){
                registerController.sendJudgeRequest(str_identifyCode,str_phone);
            }
        });
    }

    private void turnintoLoginPage(){
        NoregisterReturnButton.setOnClickListener((v)->{
            finish();
        });
    }
    private void turnintoForgetPasswordPage(){
        ForgetPasswordButton.setOnClickListener((v)->{
            Intent intent=new Intent(RegisterActivity.this, ForgetPasswordAcitivity.class);
            startActivity(intent);
        });
    }
    public void registerAndReturnLoginPage(){
        ToastUtil.showToast(context, "注册成功");
        setContentView(R.layout.register_successful);
        ReturnButton=findViewById(R.id.register_success_return);
        ReturnButton.setOnClickListener((v2)->{
            Intent intent=new Intent();
            intent.putExtra("password_return",str_password1);
            intent.putExtra("phone_return",str_phone);
            setResult(RESULT_OK,intent);
            finish();
        });
    }
}
